package app.view

import groovy.swing.SwingBuilder

import java.awt.BorderLayout

import javax.swing.JFrame
import javax.swing.JOptionPane
import javax.swing.JSplitPane
import javax.swing.JTextArea

import model.humanbody.BodyBuilder
import model.humanbody.BodyInterface
import model.scriptrunner.QuestionableInterface
import model.scriptrunner.ScriptRunner

class MainFrame implements QuestionableInterface{
	BodyInterface body
	ScriptRunner sr

	JTextArea inputArea, outputArea ;
	static main(args) {
		println "人体性态模型"
		
		def mf = new MainFrame()
		mf.show()

		//		def qs = "低热（37．4～38℃）=4 /中等度热（38．1～39℃）=5 /高热(39．1以上）=6 @ 漱水不欲咽=4 @ 神昏谵语=6 @ 吐血 # 便血 # 衄血 # 尿血=8 @ 斑色紫黑=4 @ 舌质绛=4 /舌质绛起刺=5"
		//		def qf = new QuestionFrame(qs)
		//		//qf.setLocationRelativeTo(null)
		//		def res = qf.showDialog()
		//		println res
		//qf.show()
	}

	def initBody(){
		body = BodyBuilder.build("data/data_test.xls", "主体_测试", "外部影响_测试")
	}
	
	def initScript(){
		sr = new ScriptRunner(body,"relations",this)
	}

	def MainFrame(){
		initBody()
		initScript()
	}

	def updateOutput(){
		outputArea.text = ""
		def status = body.getStatus()
		status.each{key1,value1 ->

			//println "${key}=${value}"
			//println value.class
			if(value1.class == null){
				value1.each{key2, value2 ->
					if(value2 != 0){
						outputArea.text += "${key2}:${value2}\n\n"
					}
				}
			}else if(value1 != 0){
				outputArea.text += "${key1}:${value1}\n\n"
			}
		}
	}

	def show(){
		def swing = new SwingBuilder()

		swing.frame(title: '人体性态模型:测试工具', defaultCloseOperation: JFrame.EXIT_ON_CLOSE,
		size: [800, 600], show: true, locationRelativeTo: null) {
			lookAndFeel("system")

			def resetVarsHandler = {
				body.reset()
				updateOutput()
				println "\n>重置所有变量(值归零)"
			}

			def runHandler = {
				try{
					sr.run()
				}catch(NoSuchElementException notFoundEx ){
					println "\n>异常:" + notFoundEx.getMessage()
				}catch(groovy.lang.MissingMethodException mmEx){
					println "\n>异常:没有这种方法 " + mmEx.getMethod() 
				}catch(org.codehaus.groovy.control.MultipleCompilationErrorsException mcEx){
					println "\n>异常:语法错误\n" + mcEx.getLocalizedMessage()
					//println mcEx.getStackTrace()
				}catch(Exception ex){
					println "\n>异常:" + ex.getMessage()
				}
				
				updateOutput()
			}

		
			def applyHandler = {
				println "\n>应用到人体:\n" + inputArea.text
				inputArea.text.eachLine{
					body.setCondition(it, 1)
					//println body."风热之邪_伊波拉"
				}
				updateOutput()
			}

			def reloadScriptHandler = {
				sr.reload() 
				println "\n>重载关系脚本"	
			}

			def resetModelHandler = {
				initBody()
				initScript()
				updateOutput()
				println "\n>重置整个模型"
			}

			panel(layout: new BorderLayout()){
				splitPane(orientation:JSplitPane.HORIZONTAL_SPLIT, dividerLocation:280) {
					scrollPane(constraints: "left") {
						inputArea = textArea()
						inputArea.text = "风温之邪_伊波拉"
					}
					scrollPane(constraints: "right") {
						outputArea = textArea()
						//outputArea.editable = false
					}
				}

				panel(constraints:BorderLayout.SOUTH,layout: new BorderLayout()) {
					panel(constraints:BorderLayout.CENTER) {
						button(text: "应用 ->",actionPerformed:applyHandler)
						button(text: "运行",actionPerformed:runHandler)
					}

					panel(constraints:BorderLayout.SOUTH) {
						button(text: "重置整个模型",actionPerformed:resetModelHandler)
						button(text: "重载关系脚本",actionPerformed:reloadScriptHandler)
						button(text: "重置所有变量(值归零)",actionPerformed:resetVarsHandler)

					}

					//button(text: "病因->", actionPerformed:illFrameHandler)
					//button(text: "治疗->", )
					//button(text: "人体简图",  actionPerformed:bodyFrameHandler)
					//button(text: "测试",actionPerformed:testHandler)
				}
			}
		}
	}

	@Override
	public AbstractList ask(String questions) {
		//println questions
		def qf = new QuestionFrame(questions)
		def result = qf.showDialog()

		if(result != null) println "\n>选择:\n" + result
		
		return result
	}

	@Override
	public Boolean askYesNo(String question) {
		
		def res = JOptionPane.showConfirmDialog(null, question, "诊断问题", JOptionPane.YES_NO_OPTION);
		if (res == JOptionPane.YES_OPTION){
			return true
		}
		return false;
	}


}
